package cn.iocoder.yudao.module.member.service.userusedfeedback;

import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.userusedfeedback.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.userusedfeedback.UserUsedFeedbackDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.userusedfeedback.UserUsedFeedbackMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 用户反馈 Service 实现类
 *
 * @author Alan
 */
@Service
@Validated
public class UserUsedFeedbackServiceImpl implements UserUsedFeedbackService {

    @Resource
    private UserUsedFeedbackMapper userUsedFeedbackMapper;

    @Override
    public Long createUserUsedFeedback(UserUsedFeedbackSaveReqVO createReqVO) {
        // 插入
        UserUsedFeedbackDO userUsedFeedback = BeanUtils.toBean(createReqVO, UserUsedFeedbackDO.class);
        userUsedFeedback.setUserId(getLoginUserId());
        userUsedFeedback.setStatus("未解决");
        userUsedFeedbackMapper.insert(userUsedFeedback);
        // 返回
        return userUsedFeedback.getId();
    }

    @Override
    public void updateUserUsedFeedback(UserUsedFeedbackSaveReqVO updateReqVO) {
        // 校验存在
        validateUserUsedFeedbackExists(updateReqVO.getId());
        // 更新
        UserUsedFeedbackDO updateObj = BeanUtils.toBean(updateReqVO, UserUsedFeedbackDO.class);
        userUsedFeedbackMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserUsedFeedback(Long id) {
        // 校验存在
        validateUserUsedFeedbackExists(id);
        // 删除
        userUsedFeedbackMapper.deleteById(id);
    }

    private void validateUserUsedFeedbackExists(Long id) {
        if (userUsedFeedbackMapper.selectById(id) == null) {
            throw exception(USER_USED_FEEDBACK_NOT_EXISTS);
        }
    }

    @Override
    public UserUsedFeedbackDO getUserUsedFeedback(Long id) {
        return userUsedFeedbackMapper.selectById(id);
    }

    @Override
    public PageResult<UserUsedFeedbackDO> getUserUsedFeedbackPage(UserUsedFeedbackPageReqVO pageReqVO) {
        return userUsedFeedbackMapper.selectPage(pageReqVO);
    }

}